﻿using System;
using System.Web.UI;
using System.ComponentModel;

namespace Guru.Web.Controls.Validators
{
    public class CustomValidationItem: ValidationItem
    {
        #region const
        /// <summary>
        /// Add custom function
        ///     {0}: The name of the rule, used to identify and referencing it, must be a valid javascript identifier
        ///     {1}: The actual method implementation, returning true if an element is valid. 
        ///         First argument: Current value.
        ///         Second argument: Validated element. 
        ///         Third argument: Parameters.
        ///     {2}: The default message to display for this method
        /// </summary>
        protected const string _JS_ADD_METHOD = @"
        jQuery.validator.addMethod(
            '{0}', 
            function(value, element, param) [OPEN]
               return {1}(param);
            [CLOSE],
            '{2}'
        );";

         /// <summary>
        /// Add rule
        ///     {0}:ClientID of the control
        ///     {1}: Name of the rule
        ///     {2}: Value of the rule
        ///     {3}: Custom message
        /// </summary>
        protected const string _JS_RULE = @"
            if($('#{0}') != null)
                $('#{0}').rules('add', [OPEN]
                    {1}: '{2}'
                [CLOSE]); 
                ";        
        #endregion

        protected string RuleName {
            get {
                return string.Format("{0}_{1}", this.ObjectControlToValidate.ClientID, this.ClientValidationFunction);
            }
        }

        #region Properties
        /// <summary>       
        /// The name of the custom client-side script function used for validation
        /// Parameters of the client-side script function:
        ///     clientId: id of control to validate
        /// </summary>
        [Category("Behavior"), DefaultValue(""),
        Description("ClientValidationFunction"), NotifyParentProperty(true)]
        public string ClientValidationFunction
        {
            set
            {
                ViewState["ClientValidationFunction"] = value;
            }
            get
            {
                if (ViewState["ClientValidationFunction"] == null) 
                    throw new PropertyNullRefException("ClientValidationFunction");
                return (string)ViewState["ClientValidationFunction"];
            }
        }
        #endregion

        #region Redering
        public override string RenderAdditionalMethods(HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(this.Message))
                throw new PropertyNullRefException("CustomValidationItem.Message");            
            return string.Format(_JS_ADD_METHOD, this.RuleName, this.ClientValidationFunction, this.Message);
        }
        public override string RenderTrigger(HtmlTextWriter writer)
        {
            return string.Empty;
        }
        public override string RenderRuleRegister(HtmlTextWriter writer)
        {
            string ruleValue = this.ObjectControlToValidate.ClientID;

            string basicRule = string.Format(_JS_RULE, this.ObjectControlToValidate.ToJQueryId(), this.RuleName, ruleValue);
            return basicRule.ReplaceTag();
        }
       
        #endregion
        
    }
}
